package view.listeners;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import model.Constants;
import model.IPAddress;
import view.components.IPTextArea;
import view.views.dialogContentViews.SubnetBoxPanel;
import controller.Calculator;
import controller.Controller;

public class AddSubnetFocusListener extends FocusAdapter {

	private final IPTextArea ipTextArea;
	private final SubnetBoxPanel content;
	private int hostsPossible;
	private int suffix;

	public AddSubnetFocusListener(IPTextArea ipTextArea, SubnetBoxPanel content) {
		this.ipTextArea = ipTextArea;
		this.content = content;

	}


	@Override
	public void focusLost(FocusEvent e) {
		if (e.getSource().equals(content.getSizeField())) {

			try {
				hostsPossible = Integer.parseInt(content.getSizeField()
						.getText());
				content.getSuffixField().setEditable(false);
				String ip = Calculator.getInstance().getIpFromIpTextArea(
						ipTextArea);
				if (!ip.equals(Constants.EMPTY) || ip != null) {
					IPAddress ipAddress = new IPAddress(ip);
					suffix = Calculator.getInstance().getSuffixByAddress(
							ipAddress);
					int suffixHost = Calculator.getInstance()
							.calculateSuffixByHostNumber(hostsPossible + 1);
					if (suffix < suffixHost) {
						content.getSuffixField().setText(
								Integer.toString(suffixHost));
					} else {
						content.getSuffixField().setText(
								Integer.toString(suffix));
					}
					calculateSpace(ipAddress);
				}
			} catch (NumberFormatException e1) {
				content.getSuffixField().setEditable(true);
				content.getSuffixField().setText(Constants.EMPTY);

			}
		}else{
		try {
				suffix = Integer.parseInt(content.getSuffixField()
						.getText());
				content.getSizeField().setEditable(false);
				String ip = Calculator.getInstance().getIpFromIpTextArea(
						ipTextArea);
				if (!ip.equals(Constants.EMPTY) || ip != null) {
					IPAddress ipAddress = new IPAddress(ip);
					int ipClass = Calculator.getInstance().calculateIpClass(
							ipAddress.getAddressBits(0).getBitValue());
					hostsPossible = Calculator.getInstance()
							.calculatePossibleHosts(ipClass, ipAddress) - 1;
					int hostMax = Calculator.getInstance()
							.getNumberOfMaximumHosts(suffix);
					if (hostsPossible > hostMax) {
						content.getSizeField().setText(
								Integer.toString(hostMax - 2));
					} else {
						content.getSizeField().setText(
								Integer.toString(hostsPossible));
					}
					calculateSpace(ipAddress);


				}
		} catch (NumberFormatException e1) {
				content.getSizeField().setEditable(true);
				content.getSizeField().setText(Constants.EMPTY);

		}
		}



	}

	private void calculateSpace(IPAddress ipAddress) {

		for (Integer element : Controller.getInstance().getCurrentHost()
				.getSpaces()) {
			System.out.println(element);
			if (element <= Integer.parseInt(content.getSuffixField().getText())
					&& element != 0) {
				int diff = Integer.parseInt(content.getSuffixField().getText())
						- element;
				int index = Controller.getInstance().getCurrentHost()
						.getSpaces().indexOf(element);
				Controller.getInstance().getCurrentHost().getSpaces()
						.set(index, element + diff);
				int fourthBit = ipAddress.getAddressBits(3).getBitValue() + 1;
				int thirdBit = ipAddress.getAddressBits(2).getBitValue();
				int secondBit = ipAddress.getAddressBits(1).getBitValue();
				int firstBit = ipAddress.getAddressBits(0).getBitValue();

				for (int i = 0; i < ipTextArea.getTextList().size(); i++) {
					switch (i) {
					case 0:
						ipTextArea.getTextList().get(i)
								.setText(Integer.toString(firstBit));
						break;
					case 1:
						ipTextArea.getTextList().get(i)
								.setText(Integer.toString(secondBit));
						break;
					case 2:
						ipTextArea.getTextList().get(i)
								.setText(Integer.toString(thirdBit));
						break;
					case 3:
						ipTextArea.getTextList().get(i)
								.setText(Integer.toString(fourthBit - 1));
						break;

					}
				}
				break;
			} else {
				int hosts = Calculator.getInstance().calculateSubnetSize(
						element, ipAddress) + 3;
				int fourthBit = ipAddress.getAddressBits(3).getBitValue();
				int thirdBit = ipAddress.getAddressBits(2).getBitValue();
				int secondBit = ipAddress.getAddressBits(1).getBitValue();
				int firstBit = ipAddress.getAddressBits(0).getBitValue();


				while (hosts > 0) {
					fourthBit += hosts;
					if (fourthBit > 255) {
						hosts = fourthBit - 255;
						thirdBit++;
						if (thirdBit > 255) {
							thirdBit = 0;
							secondBit++;
							if (secondBit > 255) {
								secondBit = 0;
								firstBit++;
							}
						}
					} else {
						break;
					}
				}

				for (int i = 0; i < ipTextArea.getTextList().size(); i++) {
					switch (i) {
					case 0:
						ipTextArea.getTextList().get(i)
								.setText(Integer.toString(firstBit));
						break;
					case 1:
						ipTextArea.getTextList().get(i)
								.setText(Integer.toString(secondBit));
						break;
					case 2:
						ipTextArea.getTextList().get(i)
								.setText(Integer.toString(thirdBit));
						break;
					case 3:
						ipTextArea.getTextList().get(i)
								.setText(Integer.toString(fourthBit - 1));
						break;

					}
				}
			}

		}

	}
}
